<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class Address extends Controller{
    /****
     Class register     地址
     *@
    */
    public function index(){
        $user_id = input('user_id');
        $address = Db::name('address')->where(['user_id'=>$user_id])->select();
        if($address){
           $arr = [
               'state'     => 1,
               'address'   =>$address,
               'message'   => "请求成功"
            ]; 
            
        }else{
            $arr = [
               'state'     => 0,
               'message'   => "请求失败"
            ];
        }
        return json($arr);
    }

    /****
     Class register     地址添加
     *@
    */
    public function address_add(){
        $post = input('post.');
        $data = [
          'user_id'       =>$post['user_id'],
          'name'          =>$post['name'],
          'phone'           =>$post['phone'],
          'dispatching'   =>$post['dispatching'],
          'particular'    =>$post['particular'],
          'type'          =>$post['type'],
          'addtime'       =>time(),
          'updatetime'    =>time(),
        ];
        $address = Db::name('address')->insert($data);
        if($address){
           $arr = [
               'state'     => 1,
               'message'   => "请求成功"
            ]; 
        }else{
            $arr = [
               'state'     => 0,
               'message'   => "请求失败"
            ]; 
        }
        return json($arr);
    }


    /****
     Class register     地址修改
     *@
    */
    public function address_update(){
        $post = input('post.');
        $data = [
          'id'            =>$post['id'],
          'user_id'       =>$post['user_id'],
          'name'          =>$post['name'],
          'phone'         =>$post['phone'],
          'dispatching'   =>$post['dispatching'],
          'particular'    =>$post['particular'],
          'type'          =>$post['type'],
          'updatetime'    =>time(),
        ];
        $address = Db::name('address')->update($data);
        if($address){
           $arr = [
               'state'     => 1,
               'message'   => "请求成功"
            ]; 
        }else{
            $arr = [
               'state'     => 0,
               'message'   => "请求失败"
            ]; 
        }
        return json($arr);
    }
   

   /****
     Class register     地址删除
     *@
    */
    public function delete(){
      $id = input('id');
      $delete = Db::name('address')->delete($id);
      if($delete){
           $arr = [
               'state'     => 1,
               'message'   => "请求成功"
            ]; 
        }else{
            $arr = [
               'state'     => 0,
               'message'   => "请求失败"
            ]; 
        }
        return json($arr);
    }
	/****
     Class register     默认地址  
     *@  
    */  
    public function default_address(){
      $user_id = input('user_id');
      $default_address = Db::name('user a')
			 ->join('address b','a.default_address = b.id')
			 ->where(['a.id'=>$user_id])
       ->field('default_address')
			 ->find();
      if($default_address){
           $arr = [
               'state'     			=> 1,
			         'default_address'	=>$default_address,
            ]; 
        }else{
            $arr = [
				      'state'     		=> 1,
			         'default_address'	=>'',
            ]; 
        }
        return json($arr);
    }
	
	public function update_default_address(){
      $user_id = input('user_id');
      $address_id = input('address_id');
	    $update_default_address = Db::name('user')->where(['id'=>$user_id])->update(['default_address'=>$address_id]);
      if($update_default_address){
           $arr = [
               'state'     			=> 1,
            ]; 
        }else{
            $arr = [
				'state'     		=> 0,
            ]; 
        }
        return json($arr);
    }
    
}